<template>
  <div class="index-container">
    <div class="main-header clearfix">
      <el-row>
       <el-col :xs="8" :sm="8" :md="8" :lg="8" :xl="8">
          <el-button type="primary"  icon="el-icon-plus" class="sureBtn"  size="medium"  @click="handleAdd" > 新 增 活 动</el-button>
        </el-col>
        <el-col :xs="16" :sm="16" :md="16" :lg="16" :xl="16">
          <div class="search fr">
            <div>
              <el-input  v-model="selectForm.name"   size="medium"  placeholder="活动名称"></el-input>
            </div>
            <div>
              <el-select v-model="selectForm.team_id" clearable placeholder="负责站点" filterable size="medium"  style="width: 100%;">
                <el-option
                  v-for="item in teamList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </div>
            <div>
              <el-select v-model="selectForm.status" clearable placeholder="状态" filterable size="medium"  style="width: 100%;">
               <el-option label="未开始" value="1"></el-option>
               <el-option label="进行中" value="2"></el-option>
               <el-option label="已结束" value="3"></el-option>
              </el-select>
            </div>
            <el-button size="medium"   type="primary"   icon="el-icon-search"  class="fr"    @click="handleSearch" > 搜索</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <div class="main-content" style="height: calc(100vh - 250px);">
      <main-table
        :columns="tableColumns"
        :data="tableList"
        :loading="loading"
        :pagination="pagination"
        @getData="fetchTableList"
        wrapperHeight="calc(100% - 73px)"
        height="100%"
      >
      <el-table-column slot="status" label="状态">
        <template slot-scope="scope">
          <el-tag class="tag-cursor" v-if="scope.row.status == '1'">未开始</el-tag>
          <el-tag class="tag-cursor" v-else-if="scope.row.status == '2'" type="success">已开始</el-tag>
          <el-tag class="tag-cursor" v-else-if="scope.row.status == '3'" type="danger">已结束</el-tag>
        </template>
      </el-table-column>

      <el-table-column slot="servicer_num" label="员工">
        <template slot-scope="scope">
          <span style="color: green;font-weight: bold;">{{ scope.row.servicer_num }}</span>
        </template>
      </el-table-column>

      <el-table-column slot="older_num" label="老人">
        <template slot-scope="scope">
          <span style="color: green;font-weight: bold;">{{ scope.row.older_num }}</span>
        </template>
      </el-table-column>

      <el-table-column slot="action" fixed="right" label="操作" width="310">
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="edit(scope.row)">编辑/查看</el-button>
          <el-button size="mini" type="primary" @click="editservicer(scope.row)">员工</el-button>
          <el-button size="mini" type="primary" @click="editolder(scope.row)">老人</el-button>
          <el-button size="mini" type="danger" @click="cancelOrder(scope.row)">删除</el-button>
          <!-- <el-button type="text" class="button-edit-color" @click="edit(scope.row)"><i class="fa fa-edit" aria-hidden="true"></i> 编辑</el-button> -->
          <!-- <el-button type="text" class="button-view-color" @click="editservicer(scope.row)"><i class="fa fa-users" aria-hidden="true"></i> 员工</el-button> -->
          <!-- <el-button type="text" class="button-other-color" @click="editolder(scope.row)"><i class="fa fa-user" aria-hidden="true"></i> 老人</el-button> -->
        </template>
      </el-table-column>
      </main-table>
    </div>

    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="600px">
      <orderactivity-add @cancle="dialogVisible = false" :teamList="teamList" :data="ruleForm" @load="handleSearch"></orderactivity-add>
    </el-dialog>


    <!-- 老人 -->
    <el-drawer title="老人" :size="1000" :visible.sync="dialogOlderVisible" :destroy-on-close="true" :wrapperClosable="false" :modal="false" :with-header="false" direction="rtl">
      <orderactivity-older :activityid="activityid" @close="closeDrawer"></orderactivity-older>
    </el-drawer>

    <!-- 员工 -->
    <el-drawer title="员工" :size="1000" :visible.sync="dialogServicerVisible" :destroy-on-close="true" :wrapperClosable="false" :modal="false" :with-header="false" direction="rtl">
      <orderactivity-servicer :activityid="activityid" @close="closeDrawer"></orderactivity-servicer>
    </el-drawer>

  </div>
</template>
<script>
  import mainTable from "@/components/main-table/index";
  import orderactivityAdd from './components/orderactivity-add'
  import orderactivityOlder from './components/orderactivity-older'
  import orderactivityServicer from './components/orderactivity-servicer'
  import * as APIWangge from "@/api/wangge";
  import * as APIOrderactivity from '@/api/orderactivity'
  import { unixToDate } from "@/utils"
  export default {
    name: "doorserve-serve-orderactivity",
    components: {
      mainTable,
      orderactivityAdd,
      orderactivityOlder,
      orderactivityServicer
    },
    data() {
      return {
        loading: false,
        title: '添加图标',
        dialogVisible: false,
        ruleForm: {},
        currentSelectedData: [],
        pageData: {},
        selectForm: {
          page:1,
        },
        tableList:[],
        dialogOlderVisible: false,
        dialogServicerVisible: false,
        pagination: {
          current: 1,
          size: 10,
          total: 0
        },
        tableColumns: [
         {
           index: true,
           fixed: true
         },
         {
           prop: 'name',
           label: '活动名称',
           fixed: true
         },
         {
           prop: 'team_name',
           label: '活动站点'
         },
         {
           prop: 'start_time',
           label: '开始时间',
           width: 160,
           formatter (row, column, cellValue) {
             return unixToDate(cellValue)
           }
         },
         {
           prop: 'end_time',
           label: '结束时间',
           width: 160,
           formatter (row, column, cellValue) {
             return unixToDate(cellValue)
           }
         },
         {
           slot: 'servicer_num'
         },
         {
           slot: 'older_num'
         },
         {
           slot: 'status'
         },
         {
           prop: 'address',
           label: '活动地址'
         },
         {
           prop: 'content',
           label: '活动内容',
           minWidth: 150
         },
         {
           slot: 'action'
         }
        ],
        key: Math.random(),
        teamList:[],
        activityid:''
      };
    },
    created() {},
    beforeDestroy() {},
    mounted() {
      this.fetchTableList();
      this.teamIndex();
    },
    methods: {
      // 站点列表
      teamIndex () {
        APIWangge.teamList({ page_size: 100 }).then(res => {
          this.teamList = res['data']['list']
        })
      },
      handleSearch() {
        this.pagination = {
          current: 1,
          size: 10,
          total: 0
        }
        this.fetchTableList({ page: 1 })
      },
      fetchTableList(pageObj) {
        this.loading = true;
        let data = {
          ...this.selectForm,
          ...pageObj
        }
        if(data.hasOwnProperty('status') && !data.status){
          delete data.status;
        }
        APIOrderactivity.orderactivityList(data).then((resp) => {
          if (resp.code === 200) {
            const { list = [], total = 0 } = resp.data;
            this.tableList = list || []
            this.pagination.total = total;
          }
        }).finally(() => {
        this.loading = false
      });
      },
      cancel(formName) {
        this.$refs[formName].resetFields();
        this[formName] = {};
        this.dialogVisible = false;
      },
      edit (row) {
        this.title = '编辑活动';
        this.key = Math.random();
        this.dialogVisible = true
        this.ruleForm = { ...row }
      },
      handleAdd() {
        this.title = '新增活动';
        this.dialogVisible = true;
        this.key = Math.random();
        this.ruleForm = { admin_id:'' };
      },
      cancelOrder(row) {
        this.$confirm("确定删除该条数据, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            APIOrderactivity.orderactivityDelete({ id: row.id }).then((resp) => {
              if (resp.code === 200) {
                this.$message({ type: "success", message: "删除成功!"});
                this.fetchTableList();
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "取消操作",
            });
          });
      },
      // 活动老人
      editolder (row) {
        this.activityid = String(row.id);
        this.dialogOlderVisible = true;
      },
      editservicer (row) {
        this.activityid = String(row.id);
        this.dialogServicerVisible = true;
      },
      closeDrawer(){
        this.dialogOlderVisible = false;
        this.dialogServicerVisible = false;
        this.fetchTableList();
      }
    },
  };
</script>
<style lang="scss" scoped>
  .index-container {
    box-sizing: border-box;
    width: 100%;
    padding: 0 30px !important;
    margin: 0 !important;
    background: #fff !important;

    .main-header {
      padding-top: 4px;
      margin-bottom: 24px;

      .search {
        display: inline-block;

        .search-title {
          flex: 0 0 48px;
          width: 48px;
          font-size: 16px;
          line-height: 36px;
          color: #333;
        }
      }

      .search > div {
        display: inline-block;
        margin-left: 20px;
      }
    }
    .task-icon-img{
      width: 80px;
      height: 80px;
    }
  }

  .form-footer {
    padding-left: 100px;
  }

  .dialog-main {
    .table-do {
      margin-bottom: 15px;
    }

    ::v-deep {
      th {
        .cell {
          color: #fff;
        }
      }
    }
  }
</style>
